package driver

import (
	"github.com/bwmarrin/snowflake"
	"log"
)

type GeneratorOptions struct {
	Node int64 `yaml:"node"`
}

var generatorNode *snowflake.Node
var generatorOptions *GeneratorOptions

func GeneratorInit(node int64) {
	generatorOptions = &GeneratorOptions{Node: node}
	_ = GeneratorInstance()
}

func GeneratorInstance() *snowflake.Node {
	if generatorNode != nil {
		return generatorNode
	}
	if generatorOptions == nil {
		log.Fatal("ID生成器配置不正确")
	}
	var err error
	generatorNode, err = snowflake.NewNode(generatorOptions.Node)
	if err != nil {
		log.Fatal("ID生成器配置不正确: " + err.Error())
	}
	return generatorNode
}
